#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>

void failure(char* s)
{
	perror(s);
	exit(1);
}

void printpos(char* string, int fildes)
{
	long pos;
	if( (pos = lseek(fildes, 0L, SEEK_CUR) ) < 0L) { // SEEK_CUR = 1
		failure("lseek failed");
	}
	printf("%s:%ld \n", string, pos);
}

int main()
{
	int fd; // file description
	int pid; // process id
	char buf[10]; // data refresh area
	
	// open file
	if( (fd=open("data", O_RDONLY)) < 0) {
		failure("open failed");
	}
	read(fd, buf, 10); // read file
	printpos("Before fork", fd);
	
	// fork new process
	if( (pid = fork()) <0) {
		failure("fork failed");
	}
	else if(!pid) { // child process
		printpos("child before read", fd);
		read(fd, buf, 10);
		printpos("child after read", fd);
	} else { // father process
		// wait for the end of child process
		wait(NULL);
		printpos("parent after wait", fd);
	}
}
